package cnsatmsmart.automation.mmi.controller.Buffer;

import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;
import cnsatmsmart.automation.mmi.model.data.FlightPlan;
import cnsatmsmart.automation.mmi.model.data.TSafe.Fix;
import cnsatmsmart.automation.mmi.model.database.DataBase;
import cnsatmsmart.automation.mmi.view.layers.FixesLayer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class FlightPlanList {

    /* Atribut - atribut yang dimiliki oleh kelas FlightPlanList */
        private List < FlightPlan > flightPlanList = new ArrayList < FlightPlan > ();
        private static String db_Connection = "jdbc:mysql:///MMI?user=" + GlobalEnvVars.db_Username + "&password=" + GlobalEnvVars.db_Password;

    /* Getter */
        /**
         * mengembalikan fix dari fixes dengan diberikan ID
         * @param fixes list dari sejumlah Fix
         * @param ID id dari fix yang hendak dicari
         * @return Fix - jika ditemukan <br>
         *         null - jika tidak ditemukan
         */
        private Fix getFix (List < Fix > fixes, String ID)
        {
            Iterator < Fix > FixesIter = fixes.iterator();
            while (FixesIter.hasNext())
            {
                Fix fix = FixesIter.next();
                if (fix.getID().equals(ID))
                    return (fix);
            }
            return (null);
        }

        /**
         * me-retrieve tabel Airport, Fixes, dan Navaids dari database
         * @return List < FlightPlan > - List FlightPlan yang merupakan rute perjalanan suatu pesawat
         */
        private List < FlightPlan > getFlightPlanFromDB()
        {
            List < FlightPlan > retval = new ArrayList < FlightPlan > ();
            List < Fix > allFixes = new ArrayList < Fix > ();
            allFixes.addAll(FixesLayer.getAirports());
            allFixes.addAll(FixesLayer.getFixes());
            allFixes.addAll(FixesLayer.getNavaids());

            try
            {
                DataBase db = new DataBase(db_Connection);
                db.createQuery("SELECT * FROM flightplan");

                while (db.getResultSet().next())
                {
                    FlightPlan flightplan = new FlightPlan(db.getResultSet().getString("ID_Flight"));
                    String stringPath = db.getResultSet().getString("Path_FlightPlan");
                    String[] path = stringPath.split(";");
                    for (String str : path)
                    {
                        Fix f = getFix(allFixes, str);
                        if (f!=null)
                            flightplan.addFix(f);
                    }
                    retval.add(flightplan);
                }
            }catch(Exception e)
            {
                System.out.println("failed to connect to database");
            }

            return retval;
        }

        /**
         * mengembalikan list dari FlightFlan
         * @return List < FlightPlan > - list dari FlightPlan
         */
        public List < FlightPlan > getFlightPlanList()
        {
            return (this.flightPlanList);
        }

        /**
         * mengembalikan rute flightplan dengan diberikan callSign
         * @param callSign Call Sign dari pesawat yang hendak dicari
         * @return FlightPlan - jika ditemukan <br>
         *         null - jika tidak ditemukan atau list kosong
         */
        public FlightPlan getFlightPlan (String callSign)
        {
            if (this.flightPlanList == null) {
                return (null);
            }

            String temp = callSign;
            if (temp!=null)
                temp = temp.replaceAll("\\s+","");

            Iterator flightsIter = this.flightPlanList.iterator();
            
            while (flightsIter.hasNext()) {
                FlightPlan flightplan = (FlightPlan) flightsIter.next();
                if (flightplan.getID().equals(temp)) {
                    return (flightplan);
                }
            }

            return (null);
        }

    /* Method - method lain yang dimiliki oleh kelas FlightPlanList */
        /**
         * me-retrieve data pada tabel flightplan di database
         */
        public void updateFlightPlan()
        {
            this.flightPlanList = getFlightPlanFromDB();
        }
}
